package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShopService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .createTime(LocalDateTime.now())
                .userId(BaseContext.getCurrentId())
                .build();

        if(shoppingCart.getSetmealId() == null){
            if(shoppingCartMapper.getDishByDishId(shoppingCart.getDishId(), shoppingCart.getDishFlavor())!=null && shoppingCartMapper.getDishByDishId(shoppingCart.getDishId(), shoppingCart.getDishFlavor()).getDishFlavor().equals(shoppingCart.getDishFlavor()) ){
                shoppingCartMapper.update(shoppingCart.getDishId());
                return;
            }
            DishVO dishVO = dishMapper.getById(shoppingCart.getDishId());
            shoppingCart.setName(dishVO.getName());
            shoppingCart.setImage(dishVO.getImage());
            shoppingCart.setAmount(dishVO.getPrice());
            shoppingCart.setNumber(1);
            shoppingCartMapper.add(shoppingCart);
            return;
        }

        if(shoppingCartMapper.getStemealBySetmealId(shoppingCartDTO.getSetmealId())!=null){
            shoppingCartMapper.update_setm(shoppingCart.getSetmealId());
            return;
        }
        SetmealVO setmealVO = setmealMapper.getById(shoppingCart.getSetmealId());
        shoppingCart.setName(setmealVO.getName());
        shoppingCart.setImage(setmealVO.getImage());
        shoppingCart.setAmount(setmealVO.getPrice());
        shoppingCart.setNumber(1);
        shoppingCartMapper.add(shoppingCart);

    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }


    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        if(shoppingCartDTO.getDishId()!=null){
            if(shoppingCartMapper.getDishByDishId(shoppingCartDTO.getDishId(),shoppingCartDTO.getDishFlavor()).getNumber()>1){
                shoppingCartMapper.sub_one(shoppingCartDTO.getDishId(),shoppingCartDTO.getDishFlavor());
                return;
            }
            shoppingCartMapper.deleteById(shoppingCartDTO.getDishId(),shoppingCartDTO.getDishFlavor());
            return;
        }
        if (shoppingCartMapper.getStemealBySetmealId(shoppingCartDTO.getSetmealId()).getNumber()>1){
            shoppingCartMapper.sub_one_setm(shoppingCartDTO.getSetmealId());
            return;
        }
        shoppingCartMapper.deleteById_setm(shoppingCartDTO.getSetmealId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
}
